package com.haima.ziliao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PKongxian;
import com.haima.ziliao.service.IPKongxianService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 空闲时间Controller
 * 
 * @author hima
 * @date 2024-12-10
 */
@RestController
@RequestMapping("/ziliao/kongxian")
public class PKongxianController extends BaseController
{
    @Autowired
    private IPKongxianService pKongxianService;

    /**
     * 查询空闲时间列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:kongxian:list')")
    @GetMapping("/list")
    public TableDataInfo list(PKongxian pKongxian)
    {
        startPage();
        List<PKongxian> list = pKongxianService.selectPKongxianList(pKongxian);
        return getDataTable(list);
    }


    @GetMapping("/editKongxian")
    public AjaxResult editKongxian(String data,Long tid)
    {
        pKongxianService.remove(new QueryWrapper<PKongxian>().eq("uid", SecurityUtils.getUserId()).eq("tid",tid));
        JSONArray jsonArray = JSON.parseArray(data);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);


            PKongxian kongxian = new PKongxian();
            kongxian.setUid(SecurityUtils.getUserId());
            kongxian.setTid(tid);
            kongxian.setWeek(jsonObject.getString("week"));
            kongxian.setShangwu(jsonObject.getInteger("shangwu"));
            kongxian.setXiawu(jsonObject.getInteger("xiawu"));
            kongxian.setWanshang(jsonObject.getInteger("wanshang"));
            pKongxianService.insertPKongxian(kongxian);
        }
        return AjaxResult.success("ok");
    }

    @GetMapping("/getKongxian")
    public AjaxResult getKongxian()
    {
        PKongxian pKongxian = new PKongxian();
        pKongxian.setUid(SecurityUtils.getUserId());
        List<PKongxian> list = pKongxianService.selectPKongxianList(pKongxian);
        return AjaxResult.success(list);
    }

    @GetMapping("/getKongxianByTid")
    public AjaxResult getKongxian(Long tid)
    {
        PKongxian pKongxian = new PKongxian();
        pKongxian.setTid(tid);
        List<PKongxian> list = pKongxianService.selectPKongxianList(pKongxian);
        return AjaxResult.success(list);
    }

    /**
     * 导出空闲时间列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:kongxian:export')")
    @Log(title = "空闲时间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PKongxian pKongxian)
    {
        List<PKongxian> list = pKongxianService.selectPKongxianList(pKongxian);
        ExcelUtil<PKongxian> util = new ExcelUtil<PKongxian>(PKongxian.class);
        util.exportExcel(response, list, "空闲时间数据");
    }

    /**
     * 获取空闲时间详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:kongxian:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pKongxianService.selectPKongxianById(id));
    }

    /**
     * 新增空闲时间
     */
    @PreAuthorize("@ss.hasPermi('ziliao:kongxian:add')")
    @Log(title = "空闲时间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PKongxian pKongxian)
    {
        return toAjax(pKongxianService.insertPKongxian(pKongxian));
    }

    /**
     * 修改空闲时间
     */
    @PreAuthorize("@ss.hasPermi('ziliao:kongxian:edit')")
    @Log(title = "空闲时间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PKongxian pKongxian)
    {
        return toAjax(pKongxianService.updatePKongxian(pKongxian));
    }

    /**
     * 删除空闲时间
     */
    @PreAuthorize("@ss.hasPermi('ziliao:kongxian:remove')")
    @Log(title = "空闲时间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pKongxianService.deletePKongxianByIds(ids));
    }
}
